<?php
class EateryController extends Controller
{

    public function filters()
    {
        return array(
            //'accessControl', // perform access control for CRUD operations
            array('application.components.CLoginFilter'),
        );
    }

	/**
	 * Declares class-based actions.
	 */
	public function actions() {
		return array();
	}

    public function accessRules()
    {
        return array(
            array('allow',  // allow all users to perform 'index' and 'view' actions
                'actions'=>array('list','search'),
                'users'=>array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array('gethomeinfo','update'),
                'users'=>array('@'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

	public function actionList($start=0)
	{
		$query = array(
		    'limit'=>_LIMITED_PAGE_SIZE,
		    'offset'=>(int)$start,
		    'select'=>array('url','name','desc','icon'),
            'sort'=>array('_id'=>EMongoCriteria::SORT_ASC),
		);
		$result = Eatery::model()->findAll($query);
		foreach ($result as $key => &$value) {
			$value['url'] = 'http://115.29.161.76/tao/eatery/'.$value['url'];
            unset($value['_id']);
		}
		echoToMobile($result);
	}

    public function actionSearch($key,$start=0)
    {
        $query = array(
            'conditions' => array(
                'name'=>array('=='=>new MongoRegex("/^{$key}/i"))
               // 'channel'=>array('=='=>constant("channel_homepage"))
            ),
            'limit'=>_LIMITED_PAGE_SIZE,
            'offset'=>(int)$start,
            'select'=>array('url','name','desc','icon'),
            'sort'=>array('_id'=>EMongoCriteria::SORT_ASC),
        );
        $result = Eatery::model()->findAll($query);
        foreach ($result as $key => &$value) {
            $value['url'] = 'http://115.29.161.76/tao/eatery/'.$value['url'];
            unset($value['_id']);
        }
        echoToMobile($result);
    }
}